﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Navigation;
using Microsoft.Phone.Controls;
using Microsoft.Phone.Shell;
using Microsoft.Phone.Maps.Controls;
using System.Windows.Media.Imaging;
using System.Windows.Media;
using System.Device.Location;
using Microsoft.Phone.Maps.Services;

namespace Mapping_Demo
{
    public partial class ScavengerHunt : PhoneApplicationPage
    {
        List<GeoCoordinate> wayPoints;
        
        bool isWalking = true;

        RouteQuery routeQuery;
        MapRoute currentMapRoute;

        public ScavengerHunt()
        {
            InitializeComponent();
            this.Loaded += ScavengerHunt_Loaded;
            wayPoints = new List<GeoCoordinate>();            

        }

        void ScavengerHunt_Loaded(object sender, RoutedEventArgs e)
        {
            MapLayer ml = new MapLayer();
            AddCheckBoxToMap(ml, "/Assets/LocationIcons/LocationIcon1.png", 47.610213718421726, -122.33611106872559);
            AddCheckBoxToMap(ml, "/Assets/LocationIcons/LocationIcon2.png", 47.61553698503071, -122.31551170349121);
            AddCheckBoxToMap(ml, "/Assets/LocationIcons/LocationIcon3.png", 47.615421267605434, -122.28821754455566);
            AddCheckBoxToMap(ml, "/Assets/LocationIcons/LocationIcon4.png", 47.60060732292067, -122.33181953430176);
            AddCheckBoxToMap(ml, "/Assets/LocationIcons/LocationIcon5.png", 47.589088897105114, -122.22659111022949);
            AddCheckBoxToMap(ml, "/Assets/LocationIcons/LocationIcon6.png", 47.61588413576999, -122.20195770263672);
            AddCheckBoxToMap(ml, "/Assets/LocationIcons/LocationIcon7.png", 47.64179821384579, -122.13020324707031);
            AddCheckBoxToMap(ml, "/Assets/LocationIcons/LocationIcon8.png", 47.677871412038485, -122.30589866638184);
            AddCheckBoxToMap(ml, "/Assets/LocationIcons/LocationIcon9.png", 47.610503040314235, -122.34246253967285);
            scavangeMap.Layers.Add(ml);
        }

        private void AddRadioButtonToMap(MapLayer ml, string iconSource, double lat, double lon)
        {
            Uri uri = new Uri(iconSource, UriKind.Relative);
            ImageSource img = new BitmapImage(uri);
            Image i = new Image();
            i.Source = img;
            i.Stretch = Stretch.Fill;
            i.Height = 86;
            i.Width = 86;
            RadioButton rb = new RadioButton();
            rb.Content = i;
            rb.Style = this.Resources["IconRadioButtonStyle"] as Style;
            rb.GroupName = "scavange";
            MapOverlay mo = new MapOverlay();
            mo.GeoCoordinate = new System.Device.Location.GeoCoordinate(lat, lon);
            mo.Content = rb;
            ml.Add(mo);
        }

        private void AddCheckBoxToMap(MapLayer ml, string iconSource, double lat, double lon)
        {
            Uri uri = new Uri(iconSource, UriKind.Relative);
            ImageSource img = new BitmapImage(uri);
            Image i = new Image();
            i.Source = img;
            i.Stretch = Stretch.Fill;
            i.Height = 86;
            i.Width = 86;
            CheckBox cb = new CheckBox();
            cb.Content = i;
            cb.Tag = new GeoCoordinate(lat, lon);
            cb.Style = this.Resources["IconCheckBoxStyle"] as Style;
            cb.Checked += cb_Checked;
            cb.Unchecked += cb_Unchecked;
            MapOverlay mo = new MapOverlay();
            mo.GeoCoordinate = new System.Device.Location.GeoCoordinate(lat, lon);
            mo.Content = cb;
            ml.Add(mo);
        }

        void cb_Unchecked(object sender, RoutedEventArgs e)
        {
            CheckBox cb = (CheckBox)sender;
            GeoCoordinate removeMe = (GeoCoordinate)cb.Tag;
            if (wayPoints.Contains(removeMe))
                wayPoints.Remove(removeMe);
            CreateRoute();
        }

        void cb_Checked(object sender, RoutedEventArgs e)
        {
            CheckBox cb = (CheckBox)sender;
            GeoCoordinate addMe = (GeoCoordinate)cb.Tag;
            wayPoints.Add(addMe);
            CreateRoute();
        }

        private void SetTravelToWalk(object sender, RoutedEventArgs e)
        {
            isWalking = true;
            CreateRoute();
        }

        private void SetTravelToCar(object sender, RoutedEventArgs e)
        {
            isWalking = false;
            CreateRoute();
        }

        private void CreateRoute()
        {
            if (wayPoints != null && wayPoints.Count > 1)
            {
                if (routeQuery == null)
                {
                    routeQuery = new RouteQuery();
                    routeQuery.QueryCompleted += routeQuery_QueryCompleted;
                }
                if (!routeQuery.IsBusy)
                {
                    if (isWalking)
                        routeQuery.TravelMode = TravelMode.Walking;
                    else
                        routeQuery.TravelMode = TravelMode.Driving;
                    
                    routeQuery.Waypoints = wayPoints;
                    routeQuery.QueryAsync();

                    routeProgressBar.Visibility = Visibility.Visible;
                    routeProgressBar.IsIndeterminate = true;
                }
            }
        }

        void routeQuery_QueryCompleted(object sender, QueryCompletedEventArgs<Route> e)
        {
            routeProgressBar.Visibility = Visibility.Collapsed;
            routeProgressBar.IsIndeterminate = false;
            if (null == e.Error)
            {
                Route MyRoute = e.Result;
                if (currentMapRoute != null)
                    scavangeMap.RemoveRoute(currentMapRoute);
                currentMapRoute = new MapRoute(MyRoute);
                scavangeMap.AddRoute(currentMapRoute);
                scavangeMap.SetView(currentMapRoute.Route.BoundingBox);
            }
            else
            {
                if (e.Error.Message.Contains("0x80042322") ||
                    e.Error.Message.Contains("0x80042323") ||
                    e.Error.Message.Contains("0x80042327"))
                {
                    MessageBoxResult mbr = MessageBox.Show("We couldn't find a good walk-able route. Would you like to map that route via car?", "Unwalkable Route", MessageBoxButton.OKCancel);
                    if (mbr == MessageBoxResult.OK)
                    {
                        isWalking = false;
                        carButton.IsChecked = true;
                    }
                }
                else
                {
                    MessageBox.Show("We had an unanticipated error: \n " + e.Error.Message + " \n Don't panic.", "Unknown Error", MessageBoxButton.OK);
                }
            }
        }
    }
}